import { createRouter, createWebHistory } from "vue-router";
import { request } from "../utils/request";
import manage from "../views/manage/Manage.vue";
import managehome from "../views/manage/Home.vue";
import manageuser from "../views/manage/User.vue";
import manageperson from "../views/Person.vue";
import managefile from "../views/manage/file/File.vue";
import manageavatar from "../views/manage/file/Avatar.vue";
import managecarousel from "../views/manage/good/Carousel.vue";
import managecategory from "../views/manage/good/Category.vue";
import managegood from "../views/manage/good/Goods.vue";
import managegoodInfo from "../views/manage/good/GoodInfo.vue";
import manageorder from "../views/manage/Order.vue";
import manageincomeChart from "../views/manage/income/IncomeChart.vue";
import manageincomeRank from "../views/manage/income/IncomeRank.vue";
import login from "../views/Login.vue";
import register from "../views/Register.vue";
import notFound from "../views/404NotFound.vue";

const routes: any = [
  //后台
  {
    path: "/manage",
    name: "manage",
    component: manage,
    meta: { title: "后台", path: "后台", requireAuth: true },
    children: [
      {
        path: "/manage",
        redirect: () => {
          return "/manage/homeMassage";
        },
      },
      {
        path: "user",
        name: "user",
        meta: {
          title: "用户管理",
          path: "系统管理/用户管理",
          requireAuth: true,
        },
        component: manageuser,
      },
      {
        path: "homeMassage",
        name: "homeMassage",
        meta: { title: "主页", path: "主页", requireAuth: true },
        component: managehome,
      },
      {
        path: "manageperson",
        name: "manageperson",
        meta: { title: "个人信息", path: "个人信息", requireAuth: true },
        component: manageperson,
      },
      {
        path: "file",
        name: "file",
        meta: { title: "文件管理", path: "文件/文件管理", requireAuth: true },
        component: () => managefile,
      },
      {
        path: "avatar",
        name: "avatar",
        meta: { title: "头像管理", path: "文件/头像管理", requireAuth: true },
        component: manageavatar,
      },
      {
        path: "carousel",
        name: "carousel",
        meta: {
          title: "轮播图管理",
          path: "商品/轮播图管理",
          requireAuth: true,
        },
        component: managecarousel,
      },
      {
        path: "category",
        name: "category",
        meta: {
          title: "商品分类管理",
          path: "商品/商品分类管理",
          requireAuth: true,
        },
        component: managecategory,
      },
      {
        path: "good",
        name: "good",
        meta: { title: "商品管理", path: "商品/商品管理", requireAuth: true },
        component: managegood,
      },
      {
        path: "goodInfo",
        name: "goodInfo",
        meta: {
          title: "商品管理",
          path: "商品/商品管理/商品信息",
          requireAuth: true,
        },
        component: managegoodInfo,
      },
      {
        path: "order",
        name: "order",
        meta: { title: "订单管理", path: "商品/订单管理", requireAuth: true },
        component: manageorder,
      },
      {
        path: "incomeChart",
        name: "incomeChart",
        meta: { title: "收入图表", path: "营收/收入图表", requireAuth: true },
        component: manageincomeChart,
      },
      {
        path: "incomeRank",
        name: "incomeRank",
        meta: { title: "收入排行", path: "营收/收入排行", requireAuth: true },
        component: manageincomeRank,
      },
    ],
  },
  {
    path: "/login",
    name: "login",
    meta: {
      title: "登录",
      requireAuth: false,
    },
    component: login,
  },
  {
    path: "/register",
    name: "register",
    meta: {
      title: "注册",
      requireAuth: false,
    },
    component: register,
  },
  {
    path: "/*",
    name: "notFound",
    meta: {
      title: "找不到页面",
    },
    component: notFound,
  },
  {
    path: "/",
    redirect: () => {
      return "/manage";
    },
  },
];
const router = createRouter({
  history: createWebHistory(),
  routes,
});
router.beforeEach((to, from, next) => {
  console.log(
    "123123",
    to,
    to.path === "/manage",
    localStorage.getItem("user"),
    to.meta.requireAuth
  );

  if (to.path === "/manage") {
    let user: any = JSON.parse(localStorage.getItem("user"));
    if (!user.token) {
      next("/login");
    }
  }
  let role;
  let allow = false;
  if (to.meta.requireAuth === true || to.path === "/manage/home") {
    //在后台获得该用户的身份
    request.post("http://localhost:9191/role").then((res: any) => {
      if (res.code === "200") {
        role = res.data;
        console.log("您的身份是：" + role);
        if (role === "admin") {
          allow = true;
        } else if (role === "user") {
          // alert("您没有权限");
          allow = false;
          next("/login");
        }
      } else {
        //查询身份失败
        alert(res.msg);
        next("/login");
      }
      //放行
      if (allow === true) {
        //设置网页title
        if (to.meta.title) {
          document.title = to.meta.title as any;
        } else {
          document.title = "未知页面";
        }
        next();
      }
    });
  } else {
    //不需要判断权限
    if (to.meta.requireLogin === true) {
      if (!isLogin()) {
        next("/login");
      }
    }
    if (to.meta.title) {
      document.title = to.meta.title as any;
    } else {
      document.title = "未知页面";
    }
    next();
  }
});

function isLogin() {
  let user = localStorage.getItem("user");
  if (user) {
    return true;
  } else {
    return false;
  }
}
export default router;
